package com.gx.dto.base.result;


import cn.hutool.core.util.StrUtil;
import com.gx.dto.base.enums.ResultCodeEnum;

import java.util.Collection;

/**
 * 结果工具集
 *
 * @author Vincent Lee
 * @since 0.0.1
 */
public class Results {


    public static CommonResult buildSuccess() {
        CommonResult result = new CommonResult();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    public static CommonResult buildFailure(Integer errCode, String errMessage) {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(errCode);
        result.setMsg(errMessage);
        return result;
    }

    public static CommonResult buildFailure(Integer errCode, String errMsgTemplate, Object... params) {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(errCode);
        result.setMsg(StrUtil.format(errMsgTemplate, params));
        return result;
    }

    public static CommonResult buildFailure(ResultCodeEnum resultCodeEnum) {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMessage());
        return result;
    }


    public static <T> SingleResult<T> buildResult(T data) {
        SingleResult<T> response = new SingleResult<>();
        response.setSuccess(true);
        response.setData(data);
        response.setCode(ResultCodeEnum.SUCCESS.getCode());
        response.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        return response;
    }

    public static <T> MultiResult<T> buildMultiResult(Collection<T> data) {
        MultiResult<T> response = new MultiResult<>();
        response.setSuccess(true);
        response.setData(data);
        response.setCode(ResultCodeEnum.SUCCESS.getCode());
        response.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        return response;
    }


    public static <T> PageResult<T> buildPageResult(Collection<T> data, long totalCount) {
        PageResult<T> result = new PageResult<>();
        result.setSuccess(true);
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        result.setRecords(data);
        result.setTotal(totalCount);
        return result;
    }
}
